package com.xdtech.hotel.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.hotel.dao.DeviceDao;
import com.xdtech.hotel.model.Device;
import com.xdtech.hotel.model.Room;
import com.xdtech.hotel.protocol.manager.DeviceMonitor;
import com.xdtech.hotel.service.DeviceService;
import com.xdtech.hotel.service.RoomService;
import com.xdtech.hotel.vo.DeviceItem;
import com.xdtech.hotel.vo.RoomItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-04-04 11:06:39
 * @since 1.0
 * @see
 */
@Service
public class DeviceServiceImpl implements DeviceService {
	@Autowired
	private DeviceDao deviceDao;
	@Autowired
	private BaseDao baseDao;
	@Autowired
	private RoomService roomService;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:06:39
	 * @modified by
	 * @param entity
	 */
	public void save(Device entity) {
		deviceDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:06:39
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<Device> entities) {
		deviceDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:06:39
	 * @modified by
	 * @param entity
	 */
	public void delete(Device entity) {
		deviceDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:06:39
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		deviceDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:06:39
	 * @modified by
	 * @param id
	 * @return
	 */
	public Device get(Long id) {
		return deviceDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:06:39
	 * @modified by
	 * @return
	 */
	public List<Device> getAll() {
		return deviceDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:06:39
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> devices = null;
		long rows = 0;
		if (pg!=null) {
			Page<Device> page = deviceDao.findPage(pg,"from Device order by createTime desc", values);
			devices = BeanUtils.copyListProperties(
					DeviceItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<Device> deviceList = deviceDao.find("from Device order by id desc", values);
			devices = BeanUtils.copyListProperties(
					DeviceItem.class, deviceList);
			rows = devices.size();
		}
		results.put("total", rows);
		results.put("rows", devices);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:06:39
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateDevice(DeviceItem item) {
		Device device = null;
		if (item.getId()==null) {
			device = new Device();
		}else {
			device = deviceDao.get(item.getId());
		}
		BeanUtils.copyProperties(device, item);
		deviceDao.save(device);
		DeviceMonitor.initRcuDevice(device);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:06:39
	 * @modified by
	 * @param deviceId
	 * @return
	 */
	public DeviceItem loadDeviceItem(Long deviceId) {
		Device device = deviceDao.get(deviceId);
		DeviceItem deviceItem = new DeviceItem();
		BeanUtils.copyProperties(deviceItem, device);
		return deviceItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:06:39
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteDeviceInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:06:39
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteDeviceInfo(List<Long> deviceIds) {
		for (Long id : deviceIds) {
			delete(id);
		}
		return true;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-9下午11:45:05
	 * @modified by
	 * @param id
	 * @return
	 */
	@Override
	public DeviceItem getDeviceItemById(Long id) {
		return DeviceItem.createItem(get(id));
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-14下午10:18:59
	 * @modified by
	 * @param deviceName
	 * @param deviceSn
	 * @param deviceIp
	 * @return
	 */
	@Override
	public List<Device> checkDeviceInfo(String deviceName, String deviceSn,
			String deviceIp) {
		List<Device> devices = deviceDao.findByHql("from Device where name=? or sn=? or ip=?", deviceName,deviceSn,deviceIp);
		return devices;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-17下午9:36:28
	 * @modified by
	 * @param deviceIds
	 * @return
	 */
	@Override
	public boolean checkAllowDel(List<Long> deviceIds) {
		List<Room> rooms = roomService.getRoomByDeviceIds(deviceIds);
		return rooms.isEmpty();
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:09:24
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-5-3下午4:08:55
	 * @modified by
	 * @param ip
	 * @param sn
	 */
	@Override
	public void addAutoAddDevice(String ip, String sn) {
		Device device = new Device();
		device.setIp(ip);
		device.setSn(sn);
		device.setPort(9000+"");
		device.setName(sn);
		deviceDao.save(device);
		//初始化到监控数据里
		DeviceMonitor.initRcuDevice(device);
	}
	/* (非 Javadoc)
	 * getDeviceLinkRoomBySn
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param sn
	 * @return
	 * @see com.xdtech.hotel.service.DeviceService#getDeviceLinkRoomBySn(java.lang.String)
	 */
	@Override
	public RoomItem getDeviceLinkRoomBySn(String sn)
	{
		return roomService.loadRoomItemBySn(sn);
	}

}
